﻿using System.Text.Json.Serialization;

namespace SimpleX
{
    public static partial class JsonExtensions
    {
        public static string ToJson(this object @this, JsonSerializerOptions options = null)
        {
            options ??= new JsonSerializerOptions
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                WriteIndented = true,

                NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals |
                      JsonNumberHandling.AllowReadingFromString |
                      JsonNumberHandling.WriteAsString,
            };

            return JsonSerializer.Serialize(@this, options);
        }

        public static T ToObject<T>(this string @this)
        {
            return JsonSerializer.Deserialize<T>(@this);
        }
    }
}